package com.ct.ah.lab.service.impl;

import com.ct.ah.lab.dao.AddressDao;
import com.ct.ah.lab.entity.Address;
import com.ct.ah.lab.service.IAddressService;
import com.ct.ah.lab.service.IDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("AddressService")
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private IDistrictService districtService;

    @Override
    public void saveAddress(Address address) {
        String provinceName = districtService.getName(address.getProvinceCode());
        String cityName = districtService.getName(address.getCityCode());
        String areaName = districtService.getName(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);
        addressDao.saveAddress(address);
    }

    @Override
    public List<Address> list(Integer uid) {
        List<Address> addresses = addressDao.list(uid);
        return addresses;
    }

    @Override
    public void setAddressDefault(Integer aid){
        addressDao.setAddressDefault(aid);
    }
    @Override
    public void setAddressNotDefault(Integer aid){
        addressDao.setAddressNotDefault(aid);
    }
    @Override
    public void deleteByAid(Integer aid){
        addressDao.deleteByAid(aid);
    }

    @Override
    public Address getByAid(Integer aid){
        return  addressDao.getByAid(aid);
    }

    @Override
    public void changeAddressInfo(Address updateAddress) {
        addressDao.changeAddressInfo(updateAddress);
    }

    @Override
    public Address getDefault(Integer uid){
        List<Address> addList = addressDao.getDefault(uid);
        if (addList.size() == 0){
            addList = addressDao.list(uid);
        }
        return  addList.get(0);
    }


}
